<?php

namespace app\admin\controller;

use app\common\controller\Backend;

class Express extends Backend{
    public function __construct()
    {
        parent::__construct();

        $this->ExpressModel=model('Express');

    }

    public function index(){
        $keywords=$this->request->param('keywords','');

        // 封装一个查询条件的数值
        $where=[];

        if(!empty($keywords)){
            $where['name']=['like',"%$keywords%"];
        }

        $express=$this->ExpressModel->where($where)->paginate(10);

        $count=$this->ExpressModel->count();

        $pagestr=$express->render();

        $this->assign([
            'express'=>$express,
            'count'=>$count,
            'pagestr'=>$pagestr,
            'keywords'=>$keywords,
        ]);

        return $this->fetch();
    }

    public function add(){
        if($this->request->isPost()){
            $name=$this->request->param('name','');
            $prefix=$this->request->param('prefix','');

            if(empty($name)){
                $this->error('配送公司不能为空');
                exit;
            }
            if(empty($prefix)){
                $this->error('单号前缀不能为空');
                exit;
            }

            $expname=$this->ExpressModel->where(['name'=>$name])->find();
            if($expname){
                $this->error('该公司名称已存在，请重新输入');
                exit;
            }

            $expprefix=$this->ExpressModel->where(['prefix'=>$prefix])->find();
            if($expprefix){
                $this->error('该单号前缀已存在，请重新输入');
                exit;
            }

            $data=[
                'name'=>$name,
                'prefix'=>$prefix,
            ];

            $express=$this->ExpressModel->validate('common/Express')->save($data);

            if($express===FALSE){
                $this->error($this->ExpressModel->getError());
                exit;
            }else{
                $this->success('添加成功','admin/express/index');
                exit;
            }
        }

        return $this->fetch();
    }

    public function edit(){
        $expid=$this->request->param('expid',0);

        $express=$this->ExpressModel->find($expid);

        if(empty($express)){
            $this->error('该配送公司信息不存在');
            exit;
        }

        if($this->request->isPost()){
            $name=$this->request->param('name','');
            $prefix=$this->request->param('prefix','');

            if(empty($name)){
                $this->error('配送公司名称不能为空');
                exit;
            }

            $expname=$this->ExpressModel->where(['name'=>$name])->where('id','<>',$expid)->find();

            if($expname){
                $this->error('该配送公司已存在，请重新输入');
                exit;
            }

            if(empty($prefix)){
                $this->error('单号前缀不能为空');
                exit;
            }

            $expprefix=$this->ExpressModel->where(['prefix'=>$prefix])->where('id','<>',$expid)->find();

            if($expprefix){
                $this->error('该单号前缀已存在，请重新输入');
                exit;
            }

            $data=[
                'id'=>$expid,
                'name'=>$name,
                'prefix'=>$prefix
            ];

            $result=$this->ExpressModel->isUpdate(true)->save($data);

            if($result===FALSE){
                $this->error($this->ExpressModel->getError());
                exit;
            }else{
                $this->success('修改成功','admin/express/index');
                exit;
            }
        }

        $this->assign([
            'express'=>$express
        ]);

        return $this->fetch();
    }

    public function del()
    {
        $success = [
            'result' => false,
            'msg' => ''
        ];

        if($this->request->isAjax())
        {
            $ids = $this->request->param('ids',0);
            
            $result = $this->ExpressModel->destroy($ids);

            if($result === FALSE){
                $success['result'] = false;
                $success['msg'] = $this->ExpressModel->getError();
            }else
            {
                $success['result'] = true;
                $success['msg'] = '删除成功';
            }
            return json($success);
        }
    }
}
